package com.huang.foundation.controller;

import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import com.huang.foundation.entity.respose.CaptchaResponse;
import com.huang.foundation.service.CaptchaService;
import com.huang.utils.entity.to.CaptchaTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author HuangShen
 * @Description 图形验证码 接口
 * @create 2021-10-17 19:48
 */
@RestController
@RequestMapping("/public")
public class CaptchaController {

    @Resource
    private CaptchaService captchaService;


    @GetMapping("/captcha")
    @ApiOperation(value = "获取图形验证码")
    public HStringResult<CaptchaResponse> creatCaptcha(@RequestHeader(name = "ip") String ip) {

        CaptchaTO captchaTO = this.captchaService.creatCaptcha(ip);

        if (Objects.isNull(captchaTO)) return ResultBuilder.failHSingle("验证码获取失败");

        CaptchaResponse captchaResponse = CaptchaResponse.builder()
                .img(captchaTO.getImg())
                .type(captchaTO.getType())
                .build();
        return ResultBuilder.successHSingle(captchaResponse);

    }


}
